/**
* Group: Y1
* A flag object that represents an objective capturable by a ship.
* Usage: Ship(positionX, positionY, Team Number);
* @author Mason Kwiat
*/
package lightspeed.Backend;

public class Flag
{
	public float x, y, startX, startY;
	
	private float returnTimer; //number of frames before flag returns to given base
	/**
	* Represents the team, 0 is blue team, 1 is red team.
	*/
	public short team; 
	/**
	* attatchedShip is the reference to the ship that currently has the flag (null if not captured).
	*/
	public Ship attatchedShip; 
	
	/**
	* 	Constructor: creates a flag instance.
	*  	Sets attatched ship to null to indicate that no ship has captured the flag yet and sets timer to 100 to indicate that aif the 
	*	the flag is out of the base for more than 100 seconds, then it will be automatically returned to the base.
	*	@param t indicates the team to which the flag belongs to
	*	@param ix x coordinate of the flag's location
	*	@param iy y coordinate of the flag's location
	*/
	public Flag(int t, float ix, float iy)
	{
		team = (short)t;
		x = startX = ix;
		y = startY = iy;
		
		returnTimer = 100;
		
		attatchedShip = null;
	}
	/**
	* Gets the x coordinate of the flag
	* @return x float value of x coordinate for flag
	*/
	public float getX()
	{
		return x;
	}
	/**
	* Gets the y coordinate of the flag
	* @return y float value of y coordinate for flag
	*/
	public float getY()
	{
		return y;
	}
	/**
	* This setShip method sets the attatchedShip variable to a ship to indicate that the flag has been captured by that ship.
	* @param carryShip This variable hold the reference to the current ship which holds the flag.
	*/
	public void setShip(Ship carryShip)
	{
		attatchedShip = carryShip;
	}
	/**
	* Gets the ship which hold the flag currently, if no ship hold it then it returns null
	* @return attatchedShip the Ship that currently holds the flag, if no ship holds it then return null
	*/
	public Ship getShip()
	{
		if (attatchedShip != null)
			return attatchedShip;
		else
			return null;

	}
	/**
	* Resets the flag to its initial position and indicates that no ship currently holds it.
	* 
	*/
	public void reset()
	{
		x = startX;
		y = startY;
		attatchedShip = null;
	}
	/**
	* The method checks that if the ship that captures the flag is dead, then it sets attatchedShip to null to indicate that no ship hold it.
	* If the ship that hold the flag is not dead, then it sets the flag's x and y coordinates to the ship's x and Y coordinates.
	* If the flag is abandones, then the returnTimer starts decrementing, and once it gets to 0 then the flags resets to its initial position.
	*/
	public void updatePosition()
	{
		if(attatchedShip != null && attatchedShip.dead)
		{
			attatchedShip = null;
		}
		
		if(attatchedShip != null)
		{
			x = attatchedShip.getX();
			y = attatchedShip.getY();
		}
		
		if((x != startX || y != startY) && attatchedShip == null) //check if flag is abandoned
		{
			returnTimer--;
			if(returnTimer == 0)
			{
				x = startX;
				y = startY;
				returnTimer = 100;
			}
		}
	}
	
}